#include "serialCommand.h"
//切换状态指令
QString DORM_COMMAND = "$ACZS,DORM*hh\r\n";
QString IDLE_COMMAND = "$ACZS,IDLE*hh\r\n";
QString ACQU_COMMAND = "$ACZS,ACQU*hh\r\n" ;
QString STATE_COMMAND = "$ACZT*hh\r\n" ;//状态查询
QString command_tail = "*hh\r\n" ;
//参数查询指令
QString INFO_COMMAND = "$ACZP,I*hh\r\n";
//从给定的字符串中提取出电压、电流和时间戳这些字段的值
bool getBatVTimestampFromMessage(const QString& inputString,QString& voltage, QString& current, QString& formattedDateTime,QString& SdCardGB)
{
    if (inputString.contains("M,"))//只有mcu才反馈时间
    {
        //$AZCT,M,1681280218,DORM,38.38V,47.70mA,5.15V,175.20mA*hh
        // 使用 QString 的 split() 函数将字符串按照逗号分隔成多个子字符串
        QStringList tokens = inputString.split(",");
        // 确保 tokens 中至少有 6 个元素
            if (tokens.size() < 6)
                return false;  // 如果元素个数不足，直接返回
        // 提取电压、电流和时间戳字段的值，并添加到结果列表中
        voltage = tokens.at(4).trimmed();
        current = tokens.at(5).trimmed();
        QString timestampStr = tokens.at(2).trimmed();
       // qDebug()<<"get BatV"<<voltage<<"BatC"<<current<<"RTC:"<<timestampStr;
        // 根据所需的格式设置字符串显示格式
        qint64 timestamp = timestampStr.toLongLong();//字符串表示的整数转换为 qint64 类型的变量
        QDateTime dateTime = QDateTime::fromSecsSinceEpoch(timestamp);
        formattedDateTime = dateTime.toString("yyyy/MM/dd");
        SdCardGB="0.00GB";
        return true;
    }
    else if(inputString.contains("P")&&inputString.contains("GB"))
    {
        //$AZCT,P,1681280320,IDLE,38.40V,27.41mA,5.16V,79.30mA,LaunB:0B,120.03GB*hh 废弃了

        //"$AZCT,P,ACQU,36.08V,50.90mA,5.19V,81.70mA,LaunB:0B,ACQUSaveON,LAUNSaveOFF,ACKoff,ID:0,120.03GB*hh"2023-12-23新格式

        QString str=inputString;
        QStringList tokens = inputString.split(",");
            if (tokens.size() < 6)
                return false;

        voltage = tokens.at(3).trimmed();
        current = tokens.at(4).trimmed();
        int startIndex = str.lastIndexOf(",") + 1; // 最后一个逗号的下一个位置
        int endIndex = str.lastIndexOf("*hh");
        SdCardGB = str.mid(startIndex, endIndex - startIndex);
        return true;
    }
    else
    {
        return false ;
    }  // 如果不包含M或者GB直接返回

}

int checkSerialFrameType(const QString& SerialFrame)
{
    if(SerialFrame.contains("$AZCX,"))
           return DataFrame;
       else if(SerialFrame.contains("$AZCT,"))
           return StateFrame;
       else
           return ErrorFrame;
}
